package ro.gateway.aida.sectors;

import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

import ro.gateway.aida.db.PersistenceManager;
import ro.gateway.aida.db.PersistenceToken;
import ro.gateway.aida.obj.AIDASector;
import ro.gateway.aida.obj.db.AIDASectorPersMan;
import ro.gateway.aida.usr.Arbiter;
import ro.gateway.aida.usr.Perms;
import ro.gateway.aida.usr.User;
import ro.gateway.aida.usr.UserUtils;
import ro.gateway.aida.utils.HttpUtils;
import ro.xblue.translator.ChangeLanguageTag;
import ro.xblue.translator.LanguageBean;
import ro.xblue.translator.TranslatorUtils;



/**
 * Created by IntelliJ IDEA.
 * User: xblue
 * Date: Dec 15, 2003
 * Time: 10:23:06 AM
 * To change this template use Options | File Templates.
 */
public class EditSectorAction extends Action
{
      public ActionForward execute(
            ActionMapping mapping,
            ActionForm _form,
            HttpServletRequest request,
            HttpServletResponse response )
            throws Exception
      {
            ServletContext application = this.getServlet().getServletContext();
            PersistenceToken token = PersistenceManager.tokenLookup( application );
            Arbiter ar = Arbiter.getManager( token );
            User logged_user = UserUtils.getLogged( request, response );
            boolean has_rights =
                  ( logged_user != null ) &&
                  ( ar.canDo(request, Perms.ADMIN_TOOLS ) );
            if ( !has_rights )
            {
                  return mapping.findForward( "norights" );
            }

            PersistenceToken pers_token = PersistenceManager.
                  tokenLookup( application );
            AIDASectorPersMan pers_man = AIDASectorPersMan.
                  getManager( pers_token );

            LanguageBean lang_bean = TranslatorUtils.
                  getCurrentLanguage( request, application );

            SectorForm form = ( SectorForm ) _form;
            form.setEvent( HttpUtils.getValidTrimedString( request, "event", null ) );
            form.lang = lang_bean.getLanguage();

            form.avail_langs = ChangeLanguageTag.
                  getAvailableLanguages( request, application );

            if ( request.getParameter( "start" ) != null )
            {
                  form.clear();
                  form.setAction( HttpUtils.getValidTrimedString( request, "action", null ) );

                  try
                  {
                        if ( "new".equals( form.action ) )
                        {
                              int parent_id = HttpUtils.getInt( request, "parent_id", -1 );
                              if ( parent_id != -1 )
                              {
                                    AIDASector parent = pers_man.getById( parent_id );
                                    form.sector.setParent( parent );
                              }
                        }
                        else if ( "edit".equals( form.action ) || "delete".equals( form.action ) )
                        {
                              int id = HttpUtils.getInt( request, "id", -1 );
                              AIDASector sector = pers_man.getById( id );
                              if ( sector == null )
                              {
                                    return mapping.findForward( "finnish" );
                              }
                              if ( sector.getParent_ro_aida_code() != -1 )
                              {
                                    sector.setParent( pers_man.getById( sector.getParent_ro_aida_code() ) );
                              }
                              form.sector = sector;
                        }
                  }
                  catch ( Exception ex )
                  {
                        ex.printStackTrace();
                        return mapping.findForward( "finnish" );
                  }

                  return mapping.findForward( "jsp_page" );
            }


            if ( "save".equals( form.event ) )
            {
                  try
                  {
                        if ( !"delete".equals( form.action ) )
                        {
                              form.populate( request );
                              String[] errors = form.isValid();
                              if ( errors != null )
                              {
                                    request.setAttribute( "errors", errors );
                                    return mapping.findForward( "jsp_page" );
                              }
                        }

                        if ( "delete".equals( form.action ) )
                        {
                              if ( !pers_man.canDelete( form.sector ) )
                              {
                                    String[] errors = new String[]{
                                          "canotdel"
                                    };
                                    request.setAttribute( "errors", errors );
                                    return mapping.findForward( "jsp_page" );
                              }
                              pers_man.delete( form.sector );
                        }
                        else if ( "edit".equals( form.action ) )
                        {
                              pers_man.update( form.sector );
                        }
                        else if ( "new".equals( form.action ) )
                        {
                              pers_man.insert( form.sector );
                        }
                  }
                  catch ( Exception ex )
                  {
                        String[] errors = new String[]{
                              "int.err",
                              ex.getMessage()
                        };
                        request.setAttribute( "errors", errors );
                        return mapping.findForward( "jsp_page" );
                  }
                  return mapping.findForward( "finnish" );
            }

            return mapping.findForward( "jsp_page" );
      }
}
